using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Comteck.Localization;

namespace Comteck.Winforms.Controls.Loading {

  /// <summary>
  /// 自定义的ajax的等待框
  /// </summary>
  [ToolboxBitmap(typeof(AjaxLoadControl))]
  public partial class AjaxLoadControl : UcBase, IAjaxLoadCommand {
    private class AjaxLoadHidder : IDisposable {
      private readonly IAjaxLoadCommand control;

      public AjaxLoadHidder(IAjaxLoadCommand control) {
        this.control = control;
      }

      public void Dispose() {
        control.HideAjax();
      }
    }


    #region fields
    private bool _transparentBG = true;//是否使用透明
    private int _alpha = 125;//设置透明度
    private readonly Form form = null;
    private readonly AjaxLoadHidder loadHidder;
    private int loadCount = 0;
    #endregion

    #region ctor

    /// <summary>
    /// 
    /// </summary>
    /// <param name="form"></param>
    public AjaxLoadControl(Form form) {
      InitializeComponent();

      SetStyle(ControlStyles.Opaque, true);
      SetStyle(ControlStyles.SupportsTransparentBackColor, true);

      var Location = new Point(this.Location.X + (this.Width - panelControl1.Width) / 2, this.Location.Y + (this.Height - panelControl1.Height) / 2);//居中
      panelControl1.Location = Location;
      panelControl1.Anchor = AnchorStyles.None;
      this.Visible = false;

      this.Dock = DockStyle.Fill;
      this.BringToFront();
      this.Invalidate();

      this.form = form;
      form.Controls.Add(this);
      loadHidder = new AjaxLoadHidder(this);
    }

    #endregion

    #region control events

    /// <summary>
    /// 自定义绘制窗体
    /// </summary>
    /// <param name="e"></param>
    protected override void OnPaint(PaintEventArgs e) {
      float vlblControlWidth;
      float vlblControlHeight;

      Pen labelBorderPen;
      SolidBrush labelBackColorBrush;

      if (_transparentBG) {
        var drawColor = Color.FromArgb(this._alpha, this.BackColor);
        labelBorderPen = new Pen(drawColor, 0);
        labelBackColorBrush = new SolidBrush(drawColor);
      } else {
        labelBorderPen = new Pen(this.BackColor, 0);
        labelBackColorBrush = new SolidBrush(this.BackColor);
      }
      base.OnPaint(e);
      vlblControlWidth = this.Size.Width;
      vlblControlHeight = this.Size.Height;
      e.Graphics.DrawRectangle(labelBorderPen, 0, 0, vlblControlWidth, vlblControlHeight);
      e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, vlblControlWidth, vlblControlHeight);
    }

    /// <summary>
    /// //v1.10 
    /// </summary>
    protected override CreateParams CreateParams {
      get {
        var cp = base.CreateParams;
        cp.ExStyle |= 0x00000020; //0x20;  // 开启 WS_EX_TRANSPARENT,使控件支持透明
        return cp;
      }
    }

    #endregion

    #region IAjaxLoadCommand

    /// <summary>
    /// 是否处于工作状态
    /// </summary>
    public bool IsWork => this.Visible && this.Enabled;

    /// <inheritdoc/>
    public IDisposable ShowAjax(bool isPrevent = true) {
      if (isPrevent && this.IsWork == true) {
        throw new MultiClickException(BaseResources.MultiClick);
      }
      if (Interlocked.Increment(ref loadCount) == 1) {
        this.Enabled = true;
        this.Visible = true;
      }
      return loadHidder;
    }

    /// <inheritdoc/>
    public void HideAjax() {
      if (Interlocked.Decrement(ref loadCount) == 0) {
        this.Visible = false;
        this.Enabled = false;
      }
    }

    #endregion

  }
}
